//
//  RNOLLaunchAdManager.m
//  RongNiuOnline
//
//  Created by apple on 2018/5/4.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLLaunchAdManager.h"

@interface RNOLLaunchAdManager()<XHLaunchAdDelegate>



@end


@implementation RNOLLaunchAdManager

+(void)load{
    [self shareManager];
}

+(RNOLLaunchAdManager *)shareManager{
    static RNOLLaunchAdManager *instance = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken,^{
        instance = [[RNOLLaunchAdManager alloc] init];
    });
    return instance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        //应用启动的时候 初始化开屏广告
        [[kNotificationCenter rac_addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil] subscribeNext:^(NSNotification * _Nullable note) {
//            [self setupLaunchAd];
        }];
    }
    return self;
}

-(void)setupLaunchAd{
    //设置你工程的启动页使用的是:LaunchImage 还是 LaunchScreen.storyboard(不设置默认:LaunchImage)
    [XHLaunchAd setLaunchSourceType:SourceTypeLaunchImage];
    
    //1.因为数据请求是异步的,请在数据请求前,调用下面方法配置数据等待时间.
    //2.设为3即表示:启动页将停留3s等待服务器返回广告数据,3s内等到广告数据,将正常显示广告,否则将不显示
    //3.数据获取成功,配置广告数据后,自动结束等待,显示广告
    //注意:请求广告数据前,必须设置此属性,否则会先进入window的的根控制器
      [XHLaunchAd setWaitDataDuration:3];
    
        //广告数据转模型
        //配置广告数据
        XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration new];
        //广告停留时间
        imageAdconfiguration.duration = 3;
        //广告frame
        imageAdconfiguration.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
        imageAdconfiguration.imageNameOrURLString = kLaunchAdImageUrl;
        //设置GIF动图是否只循环播放一次(仅对动图设置有效)
        imageAdconfiguration.GIFImageCycleOnce = NO;
        //缓存机制(仅对网络图片有效)
        //为告展示效果更好,可设置为XHLaunchAdImageCacheInBackground,先缓存,下次显示
        imageAdconfiguration.imageOption = XHLaunchAdImageDefault;
        //图片填充模式
        imageAdconfiguration.contentMode = UIViewContentModeScaleAspectFill;
        //广告点击打开页面参数(openModel可为NSString,模型,字典等任意类型)
        imageAdconfiguration.openModel = @"http://www.baidu.com";
        //广告显示完成动画
        imageAdconfiguration.showFinishAnimate =ShowFinishAnimateLite;
        //广告显示完成动画时间
        imageAdconfiguration.showFinishAnimateTime = 0.8;
        //跳过按钮类型
        imageAdconfiguration.skipButtonType = SkipTypeTimeText;
        //后台返回时,是否显示广告
        imageAdconfiguration.showEnterForeground = NO;
         //显示开屏广告
        [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
    
}

#pragma mark- <XHLaunchAdDelegate>
- (void)xhLaunchAd:(XHLaunchAd *)launchAd clickAndOpenModel:(id)openModel clickPoint:(CGPoint)clickPoint{
    RNOLNavigationController*nvc = (RNOLNavigationController*)kRootVC.childViewControllers.firstObject ;
    UIViewController *vc =  nvc.visibleViewController;
    [vc skipWebPageWithHtmlLink: openModel  title:@"百度一下"];
    NSLog(@"%@",openModel);
}


@end
